package com.wbb.controller;

import com.wbb.result.JsonResult;
import com.wbb.service.TemplateService;
import com.wbb.util.pageList;
import com.wbb.util.queryParams;
import com.wbb.vo.TypeTemplateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
public class TemplateController {
    @Autowired
    private TemplateService templateService;

    @PostMapping("/queryPage")
    public JsonResult<pageList<TypeTemplateVo>> queryPage(@RequestBody queryParams<TypeTemplateVo> params){
        pageList<TypeTemplateVo> pageList = templateService.queryPage(params);
        return JsonResult.ok().setData(pageList);
    }

    @DeleteMapping("/deleteById/{id}")
    public JsonResult<TypeTemplateVo> deleteById(@PathVariable Long id){
        templateService.deleteById(id);
        return JsonResult.ok();
    }

    @GetMapping("/updateById/{id}")
    public JsonResult<TypeTemplateVo> updateById(@PathVariable Long id){
        TypeTemplateVo templateVo = templateService.updateById(id);
        return JsonResult.ok().setData(templateVo);
    }

    @PostMapping("/save")
    public JsonResult<TypeTemplateVo> save(@RequestBody TypeTemplateVo templateVo){
        if (templateVo.getId() != null){
            templateService.updateType(templateVo);
        }else {
            templateService.addType(templateVo);
        }
        return JsonResult.ok();
    }

    @PatchMapping("/batchType")
    public JsonResult<TypeTemplateVo> batchType(@RequestBody List<Long> ids){
        templateService.batchType(ids);
        return JsonResult.ok();
    }
}
